import {
  createRouter,
  createWebHistory,
  type RouteRecordRaw,
} from "vue-router";

// 1. 导入组件（推荐懒加载）

// 2. 定义路由规则
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "",
    component: () => import("@/view/Index.vue"),
    meta: { title: "后台管理首页" }, // 可选元信息
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/view/home/Home.vue"),
        meta: { title: "后台管理首页" }, // 可选元信息
      },
      {
        path: "/user",
        name: "name",
        redirect: "userindex",
        children: [
          {
            path: "/user/list",
            name: "userindex",
            component: () => import("@/view/sys/user/index.vue"),
            meta: { title: "用户管理" }, // 可选元信息
          },
          {
            path: "/dept/list",
            name: "deptindex",
            component: () => import("@/view/sys/dept/index.vue"),
            meta: { title: "部门管理" }, // 可选元信息
          },
          {
            path: "/post/list",
            name: "postindex",
            component: () => import("@/view/sys/post/index.vue"),
            meta: { title: "岗位管理" }, // 可选元信息
          },
          {
            path: "/usergroup/list",
            name: "usergroupindex",
            component: () => import("@/view/sys/usergroup/index.vue"),
            meta: { title: "用户组管理" }, // 可选元信息
          },
        ],
      },
      {
        path: "/data",
        name: "data",
        redirect: "basedataindex",
        children: [
          {
            path: "/basedata/index",
            name: "basedataindex",
            component: () => import("@/view/data/basedata/index.vue"),
          },
          {
            path: "/belong/index",
            name: "belongindex",
            component: () => import("@/view/data/belong/index.vue"),
          },
          {
            path: "/village/index",
            name: "villageindex",
            component: () => import("@/view/data/village/index.vue"),
          },
        ],
      },
      {
        path:'/platform',
        name:'platform',
        redirect: "basedataindex",
        children: [
          {
            path: "/menu/index",
            name: "menuindex",
            component: () => import("@/view/platform/menu/index.vue"),
          },
          {
            path: "/role/index",
            name: "roleindex",
            component: () => import("@/view/platform/role/index.vue"),
          },
          {
            path: "/auth/index",
            name: "authindex",
            component: () => import("@/view/platform/auth/index.vue"),
          },
        ],
      }
    ],
  },

  {
    path: "/login",
    name: "login",
    component: () => import("@/view/home/Login.vue"),
    meta: { title: "后台管理首页" }, // 可选元信息
  },
];

// 3. 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL), // 使用 HTML5 模式（需服务器支持）
  routes,
});

// 修改路由守卫逻辑
router.afterEach((to) => {
  document.title = to.meta.title + "";
});

export default router;
